package com.party.mobile.biz.photo;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.party.common.paging.Page;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.exception.BusinessException;
import com.party.core.model.photo.PhotoProject;
import com.party.core.model.photo.PhotosTag;
import com.party.core.service.cos.CosBizService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.photo.IPhotoProjectService;
import com.party.core.service.photo.IPhotosTagService;
import com.party.core.utils.MyBeanUtils;
import com.party.mobile.biz.file.FileBizService;
import com.party.mobile.web.dto.photo.output.ProjectListOutput;
import com.party.mobile.web.dto.photo.output.TagOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Nullable;
import java.util.*;

/**
 * @Description:
 * @Author: yangshoukun
 * @Date: 2018/6/5 10:00
 */
@Service
public class PhotoBizService {

    private static Logger logger = LoggerFactory.getLogger(PhotoBizService.class);

    @Autowired
    private IPhotoProjectService photoProjectService;

    @Autowired
    private IPhotosTagService photosTagService;

    @Autowired
    private FileBizService fileBizService;

    @Autowired
    private CosBizService cosBizService;

    @Autowired
    private IProjectService projectService;
    @Autowired
    private IMemberGroupService memberGroupService;

    /**
     * 相册项目列表
     * @param memberId
     * @param page
     * @return
     */
    public List<PhotoProject> list(String memberId, Page page) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("memberId", memberId);
        List<PhotoProject> photoProjects = photoProjectService.webListPage(param, page);
        return photoProjects;
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    public PhotoProject detail(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("相册id不能为空");
        }

        // 获取详情时, 相册的浏览量增加1
        try {
            PhotoProject photoProject = photoProjectService.get(id);
            photoProject.setViewNum(photoProject.getViewNum() + 1);
            photoProjectService.update(photoProject);
        } catch (Exception e) {
            logger.error("获取详情时, 统计相册的浏览量异常", e);
        }
        return photoProjectService.get(id);
    }

    /**
     * 获取所有分类
     * @param photosId
     * @param type
     * @return
     */
    public List<TagOutput> tags(String photosId, Integer type) {
        if (StringUtils.isEmpty(photosId)) {
            throw new BusinessException("相册id不能为空");
        }
        if (null == type) {
            type = 1;
        }
        PhotosTag param = new PhotosTag();
        param.setPhotosId(photosId);
        List<PhotosTag> tags = photosTagService.list(param);
        List<TagOutput> outputs = LangUtils.transform(tags, tag -> TagOutput.transform(tag));

        List<TagOutput> result = Lists.newArrayList();

        if (type == 2) {
            Iterator<TagOutput> iterator = outputs.iterator();
            while (iterator.hasNext()) {
                TagOutput tagOutput = iterator.next();
                if (PhotosTag.ALBUM_TIME_LINE.equals(tagOutput.getName()) || PhotosTag.ALBUM_HOT.equals(tagOutput.getName())
                        || PhotosTag.ALBUM_CHOICENESS.equals(tagOutput.getName())) {
                    iterator.remove();
                }
            }
        }
        TagOutput all = new TagOutput();
        all.setId("0");
        all.setName("全部");
        result.add(all);

        result.addAll(outputs);
        return result;
    }

    /**
     * 机构的项目列表
     *
     * @param page  分页
     * @param orgId 机构id
     * @return
     */
    public List<ProjectListOutput> getOrgProjectList(Page page, String orgId) {
        PhotoProject photoProject = new PhotoProject();

        List<String> orgIds = memberGroupService.getChildWithMy(orgId);
        photoProject.setOrgIds(orgIds);
        //photoProject.setMemberId(orgId);
        List<PhotoProject> photoProjects = photoProjectService.listPage(photoProject, page);
        return transfromList(photoProjects);
    }

    /**
     * 获取我参加的众筹的机构的相册
     *
     * @param page     分页
     * @param memberId 用户id
     * @return 相册集合
     */
    public List<PhotoProject> getMyPhotoList(Page page, String memberId) {
        List<String> partnerIds = projectService.getPartnerIdByAuthorId(memberId);
        if (partnerIds.size() == 0) {
            return Collections.EMPTY_LIST;
        }
        List<PhotoProject> photoProjects = photoProjectService.batchByMember(new HashSet<>(partnerIds), page);
        return photoProjects;
    }

    /**
     * 转换输出
     * @param photoProjects
     * @return
     */
    public List<ProjectListOutput> transfromList(List<PhotoProject> photoProjects) {
        return LangUtils.transform(photoProjects, new Function<PhotoProject, ProjectListOutput>() {
            @Nullable
            @Override
            public ProjectListOutput apply(@Nullable PhotoProject input) {
                String filePath = fileBizService.getUploadCIPath(input.getMemberId(), "photo");
                ProjectListOutput listOutput = new ProjectListOutput();
                try {
                    MyBeanUtils.copyBeanNotNull2Bean(input, listOutput);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String qrCodeUrl = cosBizService.getPreviewPictureUrl(filePath + input.getId());
                listOutput.setQrCodeUrl(qrCodeUrl);
                return listOutput;
            }
        });
    }
}
